package com.webproject.action.employeeManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.webproject.core.BaseAction;
import com.webproject.core.Page;
import com.webproject.entity.emplayeeManager.Attence;
import com.webproject.entity.emplayeeManager.Employee;
import com.webproject.entity.emplayeeManager.WorkPlan;
import com.webproject.service.employeeManager.EmployeeManagerService;

public class EmployeeManagerAction extends BaseAction {

	private Page mpage;
	private List<Employee> employees;

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}

	private EmployeeManagerService employeeManagerService;

	public void setEmployeeManagerService(
			EmployeeManagerService employeeManagerService) {
		this.employeeManagerService = employeeManagerService;
	}

	public Page getMpage() {
		return mpage;
	}

	public void setMpage(Page mpage) {
		this.mpage = mpage;
	}

	public String queryEmployee() {
		log.info("---------------------queryEmployee");
		mpage = getPage();
		employeeManagerService.queryEmployee(mpage);
		setTotal(mpage);
		return SUCCESS;
	}

	public String queryAllEmployee() {
		mpage = getPage();
		mpage.setRow(10000);
		employeeManagerService.queryEmployee(mpage);
		employees = (List<Employee>) mpage.getRows();
		return SUCCESS;
	}

	public String editEmployee() {
		Employee employee = getEmployee();
		if (getOper().equals("add")) {
			this.employeeManagerService.saveEmployee(employee);
		} else if (getOper().equals("edit")) {
			this.employeeManagerService.updateEmployee(employee);
		}
		return SUCCESS;
	}

	private Employee getEmployee() {
		String id = getRequest().getParameter("id");
		String sex = getRequest().getParameter("sex");
		String name = getRequest().getParameter("name");
		String address = getRequest().getParameter("address");
		String phone = getRequest().getParameter("phone");
		String age = getRequest().getParameter("age");
		String status = getRequest().getParameter("status");
		String enterTime = getRequest().getParameter("enterTime");
//		String allWorkSalary = getRequest().getParameter("allWorkSalary");
//		String baseSalary = getRequest().getParameter("baseSalary");
		Employee e = new Employee();
		if (isNotEmpty(id)) {
			e.setId(Integer.parseInt(id));
		}
		e.setAddress(address);
		e.setAge(Integer.parseInt(age));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
			e.setEnterTime(sdf.parse(enterTime));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		e.setName(name);
		e.setPhone(phone);
		e.setSex(Integer.parseInt(sex));
		e.setStatus(Integer.parseInt(status));
//		e.setBaseSalary(Float.parseFloat(baseSalary));
//		e.setAllWorkSalary(Float.parseFloat(allWorkSalary));
		return e;
	}

	public String queryWorkPlan() {
		mpage = getPage();
		this.employeeManagerService.queryWorkPlan(mpage);
		setTotal(mpage);
		return SUCCESS;
	}

	public String editWorkPlan() {
		WorkPlan wp = getWorkPlan();
		if (getOper().equals("add")) {
			this.employeeManagerService.saveWorkPlan(wp);
		} else if (getOper().equals("edit")) {
			this.employeeManagerService.updateWorkPlan(wp);
		} else if (getOper().equals("del")) {
			this.employeeManagerService.deleteWorkPlan(wp);
		}
		return SUCCESS;
	}

	private WorkPlan getWorkPlan() {
		String id = getRequest().getParameter("id");
		String eId = getRequest().getParameter("eId");
		String workDay = getRequest().getParameter("workDay");
		String workTime = getRequest().getParameter("workTime");
		WorkPlan wp = new WorkPlan();
		if (isNotEmpty(id)) {
			wp.setId(Integer.parseInt(id));
		}
		if (isNotEmpty(eId)) {
			wp.seteId(Integer.parseInt(eId));
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			if (isNotEmpty(workDay)) {
				wp.setWorkDay(sdf.parse(workDay));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		wp.setWorkTime(workTime);
		return wp;
	}

	public String queryAttence() {
		mpage = getPage();
		this.employeeManagerService.queryAttence(mpage);
		setTotal(mpage);
		return SUCCESS;
	}

	public String editAttence() {
		Attence attence=getAttence();
		if(getOper().equals("add")){
			this.employeeManagerService.saveAttence(attence);
		}else if(getOper().equals("edit")){
			this.employeeManagerService.updateAttence(attence);
		}
		return SUCCESS;
	}

	private Attence getAttence() {
		Attence attence = new Attence();
		String id = getRequest().getParameter("id");
		String eId = getRequest().getParameter("eId");
		String startWorkTime = getRequest().getParameter("startWorkTime");
		String endWorkTime = getRequest().getParameter("endWorkTime");
		if (isNotEmpty(id)) {
			attence.setId(Integer.parseInt(id));
		}
		if (isNotEmpty(eId)) {
			attence.seteId(Integer.parseInt(eId));
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start=null;
		Date end=null;
		try {
			if (isNotEmpty(startWorkTime)) {
				start=sdf.parse(startWorkTime.replace("T", " "));
				attence.setStartWorkTime(start);
			}
			if (isNotEmpty(endWorkTime)) {
				end=sdf.parse(endWorkTime.replace("T", " "));
				attence.setEndWorkTime(end);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(start!=null && end !=null){
			float work=(end.getTime()-start.getTime())/(1000 * 60 * 60);
			float abs=work<8?8-work:0;
			attence.setWorkDuration(work);
			attence.setAbsenceDuration(abs);
		}
		return attence;
	}
}
